/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.emergency.drill.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.common.core.util.DateUtils;
import com.hopes.bizz.emergency.drill.dto.ErEmergencyDrillDTO;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.log.annotation.SysLog;
import com.hopes.bizz.emergency.drill.entity.ErDrillPlanEntity;
import com.hopes.bizz.emergency.drill.service.ErDrillPlanService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalTime;
import java.time.Month;
import java.time.Year;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 应急演练计划
 *
 * @author cx
 * @date 2023-11-06 14:27:42
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/drill/plan" )
@Tag(description = "plan" , name = "应急演练计划管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ErDrillPlanController {

    private final  ErDrillPlanService erDrillPlanService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param erDrillPlan 应急演练计划
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('drill_plan_view')" )
    public R geterDrillPlanPage(Page page, ErDrillPlanEntity erDrillPlan) {
        LambdaQueryWrapper<ErDrillPlanEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(Objects.nonNull(erDrillPlan.getOrgId()),ErDrillPlanEntity::getOrgId,erDrillPlan.getOrgId());
        if (Objects.nonNull(erDrillPlan.getPlanDrillTime())){
            int targetYear = DateUtils.getYear(erDrillPlan.getPlanDrillTime());
            Date startDate = Date.from(Year.of(targetYear).atMonth(Month.JANUARY).atDay(1).atStartOfDay(ZoneId.systemDefault()).toInstant());
            Date endDate = Date.from(Year.of(targetYear).atMonth(Month.DECEMBER).atDay(31).atTime(LocalTime.MAX).atZone(ZoneId.systemDefault()).toInstant());
            wrapper.between(ErDrillPlanEntity::getPlanDrillTime,startDate,endDate);
        }
        return R.ok(erDrillPlanService.page(page, wrapper));
    }


    /**
     * 通过id查询应急演练计划
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('drill_plan_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(erDrillPlanService.getById(id));
    }

    /**
     * 新增应急演练计划
     * @param erDrillPlan 应急演练计划
     * @return R
     */
    @Operation(summary = "新增应急演练计划" , description = "新增应急演练计划" )
    @SysLog("新增应急演练计划" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('drill_plan_add')" )
    public R save(@RequestBody ErDrillPlanEntity erDrillPlan) {
        return R.ok(erDrillPlanService.save(erDrillPlan));
    }

    /**
     * 修改应急演练计划
     * @param erDrillPlan 应急演练计划
     * @return R
     */
    @Operation(summary = "修改应急演练计划" , description = "修改应急演练计划" )
    @SysLog("修改应急演练计划" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('drill_plan_edit')" )
    public R updateById(@RequestBody ErDrillPlanEntity erDrillPlan) {
        return R.ok(erDrillPlanService.updateById(erDrillPlan));
    }

    /**
     * 通过id删除应急演练计划
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除应急演练计划" , description = "通过id删除应急演练计划" )
    @SysLog("通过id删除应急演练计划" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('drill_plan_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(erDrillPlanService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param erDrillPlan 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('drill_plan_export')" )
    public List<ErDrillPlanEntity> export(ErDrillPlanEntity erDrillPlan) {
        return erDrillPlanService.list(Wrappers.query(erDrillPlan));
    }

    /**
     * 应急演练总览
     * @param page 分页对象
     * @return
     */
    @Operation(summary = "总览查询" , description = "总览查询" )
    @GetMapping("/listOverview" )
    @PreAuthorize("@pms.hasPermission('drill_plan_view')" )
    public R getErEmergencyOverview(Page page, ErEmergencyDrillDTO dto) {
        return R.ok(erDrillPlanService.getDrillOverviewPage(page,dto));
    }
}